package com.banksystem.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.banksystem.Util.TokenUtil;
import com.banksystem.entity.News;
import com.banksystem.service.Groupservice;
import com.banksystem.service.Newsservice;

@Controller
public class stxwController {

	@Autowired
	private Newsservice ser;
	@Autowired
	private Groupservice gs;
	@Autowired
	private TokenUtil tokenUtil;
	/*
	 * 添加新闻
	 */
	@RequestMapping("addnews")
	public String addnews(Model model,HttpServletRequest request) {
		String username = tokenUtil.findUsernameBytoken(request);
		model.addAttribute("gname",username);
		return "stxw/addnews";
	}
	@RequestMapping("addnewss")
	public String addnews(String title,String gname,String master ,String content) {
		ser.addnews(title,gname,master,content);
		return "stxw/addnews";
	}
	/*
	 * 新闻管理
	 */
	@RequestMapping("new")
	public String news(HttpServletRequest request,Model model) {
		//String gname=tokenUtil.findUsernameBytoken(request);
		String gname = (String)request.getSession().getAttribute("gname");
		List<News> list;
		if(gname.equals("admin")) {
			list=ser.adminfindNewsAll();
		}else {
			list=ser.findNewsAll(gname);
		}
		model.addAttribute("newslist", list);
		return "stxw/new";
	}
	/*
	 * 删除新闻
	 */
	@RequestMapping("detelenews")
	public String detelenews(String nid) {
		ser.deletenews(nid);
		return "forward:new.do";
	}
	/*
	 * 搜索
	 */
	@RequestMapping("seach")
	public @ResponseBody List<News> seach(String seach,HttpServletRequest request) {
		String gname=tokenUtil.findUsernameBytoken(request);
		List<News> list=ser.findNewsAlllike(seach,gname);
		return list;
	}
}
